package com.hddznet.uniplatform.sm.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hddznet.uniplatform.sm.constant.ResultCodeEnum;
import com.hddznet.uniplatform.sm.entity.BusZyPreparStepEntity;
import com.hddznet.uniplatform.sm.service.IBusZyService;
import com.hddznet.uniplatform.sm.vo.Result;
import com.hddznet.uniplatform.sm.vo.request.ZyPreparStepAddReq;
import com.hddznet.uniplatform.sm.vo.request.ZyPreparStepModifyReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/bus/zy")
@Slf4j
@Api(tags = "特种作业接口API")
public class BusZyController {

	@Autowired
	private IBusZyService busZyService;
	
	@ApiOperation(value = "新增作业准备步骤接口", httpMethod = "POST")
	@PostMapping(value = "/preparStep")
	public Result<BusZyPreparStepEntity> addPreparStep(@RequestBody ZyPreparStepAddReq req) {
		if (StringUtils.isEmpty(req.getZyType())) {
			return new Result<BusZyPreparStepEntity>("作业类型不能为空");
		}
		BusZyPreparStepEntity entity = ZyPreparStepAddReq.fromEntity(req);
		busZyService.savePreparStep(entity);
		return  new Result<BusZyPreparStepEntity>(entity);
	}
	
	@ApiOperation(value = "更新作业准备步骤接口", httpMethod = "PUT")
	@PutMapping(value = "/preparStep")
	public Result<BusZyPreparStepEntity> modifyPreparStep(@RequestBody ZyPreparStepModifyReq req) {
		if (StringUtils.isEmpty(req.getId())) {
			return new Result<BusZyPreparStepEntity>("ID不能为空");
		}
		if (StringUtils.isEmpty(req.getZyType())) {
			return new Result<BusZyPreparStepEntity>("作业类型不能为空");
		}
		BusZyPreparStepEntity entity = ZyPreparStepModifyReq.fromEntity(req);
		if (busZyService.modifyPreparStep(entity)) {
			return  new Result<BusZyPreparStepEntity>(entity);
		}
		return new Result<BusZyPreparStepEntity>(ResultCodeEnum.MODIFY_DATA_ERROR, null);
	}
	
	@ApiOperation(value = "删除作业准备步骤接口", httpMethod = "DELETE")
	@DeleteMapping(value = "/preparStep")
	public Result<?> deletePreparStep(@RequestParam(name="id",required=true) String id) {
		busZyService.delPreparStep(id);
		return new Result<>();
	}
	
	@ApiOperation(value = "分页查询作业准备步骤接口", httpMethod = "GET")
	@GetMapping(value = "/preparStep/list")
	public Result<?> queryPreparStepPageList(BusZyPreparStepEntity entity,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		IPage<BusZyPreparStepEntity>  pageList = busZyService.queryPreparStep(entity, req.getParameterMap(), pageNo, pageSize);
		return new Result<>(pageList);
	}
	
}
